package com.hospital.insurancepeople.handler;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hospital.common.core.result.ErrorTip;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.common.core.utils.DateUtil;
import com.hospital.insurancepeople.module.entity.Feedetail;
import com.hospital.insurancepeople.module.entity.InpBillDetail;
import com.hospital.insurancepeople.module.entity.InpSettleMaster;
import com.hospital.insurancepeople.module.mapper.InpSettleMasterMapper;
import com.hospital.insurancepeople.module.mapper.InsuranceLogsMapper;
import com.hospital.insurancepeople.utils.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lvyao
 * @Date: 2023-04-03-14:51
 * @Description:全部住院病人费用明细上传（住院结算数据插入后）
 */
public class InpDetailUp extends AbstractMessageInfo {
    private InsuranceLogsMapper insuranceLogsMapper = SpringContextHolder.getBean(InsuranceLogsMapper.class);
    private InpSettleMasterMapper inpSettleMasterMapper = SpringContextHolder.getBean(InpSettleMasterMapper.class);

    @Override
    Tip getUpDetail(JSONObject requestVal) {
        Tip tip = new SuccessTip();
        String url = insuranceLogsMapper.selectInpDetailUrl();
        if (!ObjectUtil.isNull(url)) {
            InpSettleMaster inpSettleMaster = JSON.parseObject(requestVal.toJSONString(), InpSettleMaster.class);
            List<InpBillDetail> byPatientIdInpBillDetails = inpSettleMasterMapper.getByPatientIdInpBillDetails(inpSettleMaster.getPatientId(), String.valueOf(inpSettleMaster.getVisitId()));
            JSONObject jsonObject = new JSONObject();
            if (!byPatientIdInpBillDetails.isEmpty()) {
                String allTime = DateUtil.getAllTime();
                List<Feedetail> feedetailList = new ArrayList<>();
                for (InpBillDetail inpBillDetail:byPatientIdInpBillDetails){
                    Feedetail feedetail = new Feedetail();
                   String feedetlSn = inpBillDetail.getPatientId()+inpBillDetail.getVisitId()+allTime+inpBillDetail.getItemNo();
                    feedetail.setFeedetl_sn(feedetlSn);
                    feedetail.setInit_feedetl_sn("");
                    feedetail.setMdtrt_id("");
                    feedetail.setDrord_no("");
                    feedetail.setPsn_no("");
                    feedetail.setMed_type("");
                    feedetail.setFee_ocur_time(DateUtil.format(inpBillDetail.getBillingDateTime(),"yyyy-MM-dd HH:mm:ss"));
                    feedetail.setMed_list_codg("");
                    feedetail.setMedins_list_codg("");
                    feedetail.setDet_item_fee_sumamt(inpBillDetail.getCharges());
                    feedetail.setCnt(inpBillDetail.getAmount());
                    feedetail.setPric(inpBillDetail.getCharges().divide(inpBillDetail.getAmount()));
                    feedetail.setBilg_dept_codg(inpBillDetail.getBilgDeptCodg());
                    feedetail.setBilg_dept_name(inpBillDetail.getBilgDeptName());
                    feedetail.setBilg_dr_codg(inpBillDetail.getBilgDrCodg());
                    feedetail.setBilg_dr_name(inpBillDetail.getBilgDrName());
                    feedetail.setAcord_dept_codg("");
                    feedetail.setAcord_dept_name("");
                    feedetail.setOrders_dr_code(inpBillDetail.getBilgDrCodg());
                    feedetail.setOrders_dr_name(inpBillDetail.getBilgDrName());
                    feedetail.setHosp_appr_flag(inpBillDetail.getHospApprFlag());
                    feedetail.setTcmdrug_used_way("");
                    feedetail.setEtip_flag("");
                    feedetail.setEtip_hosp_code("");
                    feedetail.setDscg_tkdrug_flag("");
                    feedetail.setMatn_fee_flag("");
                    feedetail.setMemo("");
                    feedetail.setItem_name(inpBillDetail.getItemName());
                    feedetail.setItem_type(inpBillDetail.getItemType());
                    feedetail.setItem_spec(inpBillDetail.getItemSpec());
                    feedetail.setItem_unt(inpBillDetail.getUnits());
                    feedetail.setAdmission_number(inpBillDetail.getPatientId()+"==" + inpBillDetail.getVisitId());
                    feedetailList.add(feedetail);
                }
                jsonObject.put("feedetail", feedetailList);
                tip.setData(jsonObject);
                tip.setMessage(url);
                tip.setCode(0);
            } else {
                tip = new ErrorTip(1002, "明细费用上传接口未查询到数据！");
            }
        } else {
            tip = new ErrorTip(1001, "明细费用上传接口地址’INP_BILL_DETAIL_URL‘未配置！");
        }
        return tip;
    }
}
